package com.nongcai159.mall.lib_common.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout

import com.nongcai159.mall.lib_common.R
import com.nongcai159.mall.lib_common.impl.LimitClickHelper
import com.nongcai159.mall.lib_common.impl.ILimitClick

import java.util.ArrayList
import java.util.Arrays


/**
 * 导航栏
 *
 *
 * Created by tanbiheng on 2017/9/27.
 */

class NavigateBar : LinearLayout, ILimitClick {
    // 导航栏tab个数
    private var tabCount = 0
    // 默认状态下tab图片集合
    private lateinit var defaultImageList: MutableList<Int>
    // 选中状态下tab图片集合
    private lateinit var focusImageList: MutableList<Int>
    // tab文字集合
    private var tabTextList: List<String>? = null
    // 导航栏tab默认字体颜色
    private var defaultTextColor = 0xaaaaab
    // 导航栏tab选中字体颜色
    private var focusTextColor = 0x1000000
    // 字体大小
    private var textSize = 14f


    private var tabList: ArrayList<NavigateTab>? = null

    private var listener: OnNavigateListener? = null

    constructor(context: Context) : super(context) {
        init(null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        defaultImageList = ArrayList()
        focusImageList = ArrayList()

        val array = context.obtainStyledAttributes(attrs, R.styleable.NavigateBar)

        tabCount = array.getInt(R.styleable.NavigateBar_tabCount, 0)

        val defaultImageArrResId = array.getResourceId(R.styleable.NavigateBar_defaultImageArray, 0)
        val focusImageArrResId = array.getResourceId(R.styleable.NavigateBar_focusImageArray, 0)
        val textArrResId = array.getResourceId(R.styleable.NavigateBar_textArray, 0)

        val defaultImageTypeArray = context.resources.obtainTypedArray(defaultImageArrResId)
        if (defaultImageTypeArray != null && defaultImageTypeArray.length() > 0) {
            for (i in 0 until defaultImageTypeArray.length()) {
                defaultImageList.add(defaultImageTypeArray.getResourceId(i, 0))
            }
        }

        val focusImageTypeArray = context.resources.obtainTypedArray(focusImageArrResId)
        if (focusImageTypeArray != null && focusImageTypeArray.length() > 0) {
            for (i in 0 until focusImageTypeArray.length()) {
                focusImageList.add(focusImageTypeArray.getResourceId(i, 0))
            }
        }

        val tabTextArray = context.resources.getStringArray(textArrResId)
        if (tabTextArray != null && tabTextArray.size > 0) {
            tabTextList = Arrays.asList(*tabTextArray)
        }

        defaultTextColor = array.getColor(R.styleable.NavigateBar_defaultTextColor, 0xaaaaab)
        focusTextColor = array.getColor(R.styleable.NavigateBar_focusTextColor, 0x1000000)
        textSize = array.getDimension(R.styleable.NavigateBar_textSize, 14f)
        // 回收
        array.recycle()

        orientation = LinearLayout.HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL

        initTabs()
    }

    fun setOnNavigateListener(listener: OnNavigateListener) {
        this.listener = listener

        registerListener()
    }

    private fun registerListener() {
        if (tabList != null && tabList!!.size > 0) {
            for (tab in tabList!!) {
                tab.setOnClickListener(LimitClickHelper(this))
            }
        }
    }

    fun setFocusPosition(position: Int) {
        for (i in tabList!!.indices) {
            if (i == position) {
                tabList!![i].setImageResource(focusImageList!![i])
                tabList!![i].setTextColor(focusTextColor)
            } else {
                tabList!![i].setImageResource(defaultImageList!![i])
                tabList!![i].setTextColor(defaultTextColor)
            }
        }
    }

    private fun initTabs() {
        this.removeAllViews()
        tabList = ArrayList()

        if (tabCount > 0) {
            for (i in 0 until tabCount) {
                val navigateTab = NavigateTab(context)
                navigateTab.curPosition = i
                navigateTab.setImageResource(defaultImageList!![i])
                navigateTab.setText(tabTextList!![i])
                navigateTab.setTextSize(textSize)
                navigateTab.setTextColor(defaultTextColor)

                val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)
                navigateTab.layoutParams = params

                tabList!!.add(navigateTab)
                this.addView(navigateTab)
            }
        }
    }

    override fun onClick(view: View) {
        if (listener != null) {
            if (view is NavigateTab) {
                val position = view.curPosition
                setFocusPosition(position)
                listener!!.onClickNavigate(position)
            }
        }
    }

    interface OnNavigateListener {
        fun onClickNavigate(position: Int)
    }
}
